$(function() {
	var bedGrid = $("#bedroomDetailsGrid");
	var bedroomId= $("#bedroomId").val();
	var bed= $("#bed").val();
	var studentcount= $("#studentcount").val();
	// 初始化
	bedGrid.datagrid({
		url : 'bedroomDetails/student'+ (bedroomId ? ("/" + bedroomId) : ""),
		fit : true,
		border : false,
		fitColumns : true,
		rownumbers:true,
		emptyMsg:"<img  src='public/images/noOne.png'>",
		pagination : true,// 是否显示分页栏
		columns : [ [
				{
					field : 'name',
					title : '学员名称',
					align : 'center',
					width : 90,
				},
				{
					field : 'gender',
					title : '学员性别',
					align : 'center',
					width : 90,
					formatter: function (value, rec) {
						if(rec.gender =='女'){
							 return '<img src="public/images/female.png" title="女生">';
						}else if(rec.gender =='男'){
							 return '<img src="public/images/male.png" title="男生">';
						}
				     },
					
				},
				{
					field : 'tel',
					title : '学员电话',
					align : 'center',
					width : 90,
				},
				{
					field : 'education',
					title : '学员身份',
					align : 'center',
					width : 90,
					
				},
				{
					field : 'ac',
					title : '操作',
					width : 180,
					align : 'center',
					formatter : function(value, row, index) {
						var bts = [];
						if(row.dormitory==false){
						bts.push('<a data-id="' + row.id + '" data-index="'+ index + '" class="ctr dormitory">设为寝室长</a>');
						}
						bts.push('<a data-id="' + row.id + '" data-index="'+ index + '" class="ctr Inbedroom">换寝</a>');
						bts.push('<a data-id="' + row.id + '" data-index="'+ index + '" class="ctr delete">退寝</a>');
						
						return bts.join('');
					}
				} ] ]
	});
	
	
	//返回
	$("#edreturn").on("click",function(){
		 var tab=$("#tabs").tabs('getSelected');;
			var tabs=$("#tabs");
			var content = '<div scrolling="auto" frameborder="0"  style="width:100%;height:100%;"></div>';     
			tabs.tabs('update',{   
			  				tab:tab,
							options: {
								closable:true,
								title: '寝室列表',
								content:content, 
								 href:'/bedroom' // the new content URL
							}
                  });
		   tab.panel('refresh', '/bedroom');
			
	 });
	var gridPanel = bedGrid.datagrid("getPanel");
	gridPanel.on("click", "a.delete", function() {
		var tab = $('#tabs').tabs('getSelected'); // 获取选择的面板
		// 获取data-xxx绑定的标签属性数据
		var index = this.dataset.index;
		var id = this.dataset.id;
		$.messager.confirm("提示", "确定给该学员退寝?", function(r) {
			// 点击了确定按钮，r返回的结果为true
			if (r) {
				$.get("bedroomDetails/delete_student/"+ (id ? ("/" + id) : "")+ (bedroomId ? ("/" + bedroomId) : ""), function(rs) {
					if (rs.success) {
						$.messager.alert("提示","该学员已退出此寝室");
						// 刷新表格
						tab.panel('refresh');
						
					}else{
						$.messager.alert("提示",data.message,"info");
					}
					
				});
			}
		});
		/**
		 * 换寝室
		 */
	}).on("click", "a.Inbedroom", function() {
		
		// 获取data-xxx绑定的标签属性数据
		var index = this.dataset.index;
		var bedroomType = $("#bedroomType").val();
		var rows = bedGrid.datagrid("getRows");
		
		//学员id
		var id = this.dataset.id;
		inbedroom(id,bedroomType,bedroomId,studentcount);
		
		
	}).on("click","a.dormitory",function(){
		// 获取data-xxx绑定的标签属性数据
		var tab = $('#tabs').tabs('getSelected'); // 获取选择的面板
		var index = this.dataset.index;
		var id = this.dataset.id;
		$.messager.confirm("提示", "您确定要设置该学员为寝室长吗?", function(r) {
			// 点击了确定按钮，r返回的结果为true
			if (r) {
				$.get("bedroomDetails/dormitory/"+ (id ? ("/" + id) : "")+ (bedroomId ? ("/" + bedroomId) : ""), function(rs) {
					if (rs.success) {
						$.messager.alert("提示","设置成功！该学员已成为寝室长");
						tab.panel('refresh');
					}else{
						$.messager.alert("提示",data.message,"info");
					}
					
				});
			}
	});
	
});	
	
	/**
	 * 换寝
	 */
	function inbedroom(id,bedroomType,bedroomId,studentcount) {
		// 创建窗口
			var dialog = $("<div/>").dialog({
				href: "bedroomDetails/inbedroom"+ (id ? ("/" + id) : "")+ (bedroomType ? ("/" + bedroomType) : "")+ (bedroomId ? ("/" + bedroomId) : ""),
				title: "调换寝室(单击行即可选择寝室)",
				height: 430,
				width: 710,
				modal: true,
				onClose: function() {
					$(this).dialog("destroy");
				},
				buttons : [ {
					iconCls : 'icon-save',
					text : '确定',
					align : 'center',
					handler : function() {
									//获取所选复选框中的值
									var checkedItems = $('#SpareBedroomGrid').datagrid('getChecked');
									var ids=[];
									   $.each(checkedItems, function(index, item){
										   //获取选中学员的ID
									       ids.push(item.id);
									   });	
									   if(checkedItems.length>0){
									// serialize方法可以获取到表单所有数据
									$.post("bedroomDetails/inbedroomstu"+ (id ? ("/" + id) : "")+(ids?("/"+ids):"")+(bedroomId?("/"+bedroomId):""), function(rs) {
										if (rs.success) {
											$.messager.alert("提示",rs.message);
											//关闭窗口
											dialog.dialog("close");
											
											// 获取data-xxx绑定的标签属性数据
											 var tab = $('#tabs').tabs('getSelected'); // 获取选择的面板
											 tab.panel('refresh');
											 
											//刷新数据表格
											 bedGrid .datagrid("reload");
											
										} else {
											$.messager.alert("提示",rs.message);
										}
									});		
								 }else{
									 $.messager.alert("提示","您还没有选择要调换的寝室（单击行即可选择哦）");
								 }
								
							}	
							
					
				}
				]
	
			});
		}

});